package com.sgb.kjwl.view.ui.adapter.pricecheck;

import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pricecheck.AttributeViewEntity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 * by 2021.03.23  调整布局
 */
public class AttributeViewAdapter extends BaseRecycleAdapter<AttributeViewEntity> {

    private RecyclerView mRecyclerView;
    private AttributeChildAdapter mAttributeChildAdapter;

    private String cName;
    private String cId;
    private String[] attributeString = new String[20];//此处有问题，暂时还原原始逻辑，有待优化
    private List<AttributeViewEntity.TypeBean> typeList;
    private List<AttributeViewEntity> allDatas;
    private int lastIndex = 10;

    public AttributeViewAdapter(Context context, List<AttributeViewEntity> datas) {
        super(context, R.layout.item_attribute_item, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final AttributeViewEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        mRecyclerView = holder.getItem().findViewById(R.id.attributeChildRecyclerView);
        mAttributeChildAdapter = new AttributeChildAdapter(context, entity.getType());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(mAttributeChildAdapter);
        mAttributeChildAdapter.setItemSelect(true);
        mAttributeChildAdapter.setOnItemClick(new AdapterOnItemClick<AttributeViewEntity.TypeBean>() {
            @Override
            public void onItemClick(AttributeViewEntity.TypeBean typeBean, int position) {
                String cid = typeBean.getCid();
                allDatas = getDatas();
                for (int i = 0; i < allDatas.size(); i++) {
                    List<AttributeViewEntity.TypeBean> typeBeanList = allDatas.get(i).getType();
                    for (int j = 0; j < typeBeanList.size(); j++) {
                        if (cid.equals(typeBeanList.get(j).getCid())) {
                            typeBean.setSelected(true);
                            attributeString[i] = entity.getId() + "&" + entity.getName() + "&" + typeBean.getCid() + "&" + typeBean.getCname();
                        } else {
                            typeBean.setSelected(false);
                        }
                    }
                }
            }
        });
    }

    public void clearAttributeString() {
        Arrays.fill(attributeString, "");
    }

    public String[] getAttributeString() {
        return attributeString;
    }
}
